package excelutils;

import excelutils.config.CustomCellWriteHandler;
import com.alibaba.excel.EasyExcel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.*;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/30 17:58
 * @since JDK 1.8
 */
public class ExcelUtils {


    /**
     * 获取excel的内容
     * @param file
     * @param fileName
     * @return
     */
    public static List<Map<String,Object>> getExcelContent2(File file, String fileName){
        List<Map<String,Object>> mapList=new ArrayList<>();
        InputStream fis = null;
        try {
            fis = new FileInputStream(file);
            Workbook workbook = null;
            if (fileName.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(fis);
            } else if (fileName.endsWith(".xls") || fileName.endsWith(".et")) {
                workbook = new HSSFWorkbook(fis);
            }
            fis.close();
            /* 读EXCEL文字内容 */
            // 获取第一个sheet表，也可使用sheet表名获取
            Sheet sheet = workbook.getSheetAt(0);
            // 获取行
            Iterator<Row> rows = sheet.rowIterator();
            Row row;
            Cell cell;
            while (rows.hasNext()) {
                row = rows.next();
                // 获取单元格
                Iterator<Cell> cells = row.cellIterator();
                int num=0;
                StringBuilder stringBuilder=new StringBuilder();
                Map<String, Object> stringObjectMap=new HashMap<>();
                while (cells.hasNext()) {
                    num++;
                    cell = cells.next();
                    String cellValue = POIUtil.getCellValue(cell);
                    stringObjectMap.put(String.valueOf(num),cellValue);
                }
                mapList.add(stringObjectMap);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }  finally {
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return mapList;
    }



    /**
     *动态头部
     * @param path  路径
     * @param objectMaps 数据集合
     * @param stringObjectMap  要做字段转换的Map
     */
    public static  void  formartExcelMapListHead(String path, List<Map<String, Object>> objectMaps, Map<String,String> stringObjectMap){
        if(objectMaps==null||objectMaps.size()==0){
            return;
        }
        List<List<String>> headList = new ArrayList<List<String>>();
        Map<String, Object> map=objectMaps.get(0);
        Set<String> strings=map.keySet();
        for (String key:strings){
            List<String> stringList=new ArrayList<>(1);
            String headName=null;
            if (stringObjectMap.containsKey(key)){
                headName=stringObjectMap.get(key);
            }else {
                headName=key;
            }
            stringList.add(headName);
            headList.add(stringList);
        }
        List<List<Object>> list = new ArrayList<>();
        for (int i = 0; i < objectMaps.size() ; i++) {
            List<Object> objects=new ArrayList<>(strings.size());
            Map<String, Object> objectMap1=objectMaps.get(i);
            for (String string: strings) {
                Object v=objectMap1.get(string);
                objects.add(v);
            }
            list.add(objects);
        }
        ExcelWirte(path,headList,list);
    }

    private static void ExcelWirte(String path,List<List<String>> headList,List<List<Object>> list){
        EasyExcel.write(path)
                // 这里放入动态头
                .head(headList).sheet("模板")
                .registerWriteHandler(new CustomCellWriteHandler())
                // 当然这里数据也可以用 List<List<String>> 去传入
                .doWrite(list);
        // 记得 释放资源
    }


}
